var t, e = require("../../../@babel/runtime/helpers/typeof");

t = function() {
    function t(t) {
        for (var e = [], n = arguments.length - 1; n-- > 0; ) e[n] = arguments[n + 1];
        s.forEach(function(n, o) {
            void 0 !== e[o] && (t[n] = e[o]);
        });
    }
    function n() {
        return u || (u = wx.getSystemInfoSync()), u;
    }
    function o(t, e) {
        return t(e = {
            exports: {}
        }, e.exports), e.exports;
    }
    function r(t) {
        return function(e) {
            for (var n = [], o = arguments.length - 1; o-- > 0; ) n[o] = arguments[o + 1];
            return void 0 === e && (e = {}), new Promise(function(o, r) {
                e.success = function(t) {
                    o(t);
                }, e.fail = function(t) {
                    r(t);
                }, t.apply(void 0, [ e ].concat(n));
            });
        };
    }
    function i(t, e) {
        return void 0 === e && (e = !1), new Promise(function(n) {
            t.draw(e, n);
        });
    }
    function a(t) {
        var e = "";
        if ("string" == typeof t) e = t; else for (var n = 0; n < t.length; n++) e += String.fromCharCode(t[n]);
        return b.encode(e);
    }
    function c(t, e, n, o, r, i, c) {
        void 0 === c && (c = function() {}), void 0 === i && (i = "png"), i = function(t) {
            return "image/" + (t = t.toLowerCase().replace(/jpg/i, "jpeg")).match(/png|jpeg|bmp|gif/)[0];
        }(i), /bmp/.test(i) ? function(t, e, n, o, r, i) {
            wx.canvasGetImageData({
                canvasId: t,
                x: e,
                y: n,
                width: o,
                height: r,
                success: function(t) {
                    i(t, null);
                },
                fail: function(t) {
                    i(null, t);
                }
            });
        }(t, e, n, o, r, function(t, e) {
            var n = function(t) {
                var e = t.width, n = t.height, o = e * n * 3, r = o + 54, i = [ 66, 77, 255 & r, r >> 8 & 255, r >> 16 & 255, r >> 24 & 255, 0, 0, 0, 0, 54, 0, 0, 0 ], c = [ 40, 0, 0, 0, 255 & e, e >> 8 & 255, e >> 16 & 255, e >> 24 & 255, 255 & n, n >> 8 & 255, n >> 16 & 255, n >> 24 & 255, 1, 0, 24, 0, 0, 0, 0, 0, 255 & o, o >> 8 & 255, o >> 16 & 255, o >> 24 & 255, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ], u = (4 - 3 * e % 4) % 4, s = t.data, h = "", d = e << 2, f = n, l = String.fromCharCode;
                do {
                    for (var g = d * (f - 1), p = "", v = 0; v < e; v++) {
                        var x = v << 2;
                        p += l(s[g + x + 2]) + l(s[g + x + 1]) + l(s[g + x]);
                    }
                    for (var y = 0; y < u; y++) p += String.fromCharCode(0);
                    h += p;
                } while (--f);
                return a(i.concat(c)) + a(h);
            }(t);
            p(c) && c("data:image/" + i + ";base64," + n, e);
        }) : console.error("暂不支持生成'" + i + "'类型的base64图片");
    }
    var u = void 0, s = [ "touchstarted", "touchmoved", "touchended" ], h = {}, d = {
        id: {
            default: "cropper",
            get: function() {
                return h.id;
            },
            set: function(t) {
                "string" != typeof t && console.error("id：" + t + " is invalid"), h.id = t;
            }
        },
        width: {
            default: 750,
            get: function() {
                return h.width;
            },
            set: function(t) {
                "number" != typeof t && console.error("width：" + t + " is invalid"), h.width = t;
            }
        },
        height: {
            default: 750,
            get: function() {
                return h.height;
            },
            set: function(t) {
                "number" != typeof t && console.error("height：" + t + " is invalid"), h.height = t;
            }
        },
        pixelRatio: {
            default: n().pixelRatio,
            get: function() {
                return h.pixelRatio;
            },
            set: function(t) {
                "number" != typeof t && console.error("pixelRatio：" + t + " is invalid"), h.pixelRatio = t;
            }
        },
        scale: {
            default: 2.5,
            get: function() {
                return h.scale;
            },
            set: function(t) {
                "number" != typeof t && console.error("scale：" + t + " is invalid"), h.scale = t;
            }
        },
        bgcolor: {
            default: "",
            get: function() {
                return h.bgcolor;
            },
            set: function(t) {
                h.bgcolor = t;
            }
        },
        zoom: {
            default: 5,
            get: function() {
                return h.zoom;
            },
            set: function(t) {
                "number" != typeof t ? console.error("zoom：" + t + " is invalid") : (t < 0 || t > 10) && console.error("zoom should be ranged in 0 ~ 10"), 
                h.zoom = t;
            }
        },
        src: {
            default: "",
            get: function() {
                return h.src;
            },
            set: function(t) {
                "string" != typeof t && console.error("src：" + t + " is invalid"), h.src = t;
            }
        },
        cut: {
            default: {},
            get: function() {
                return h.cut;
            },
            set: function(t) {
                "object" !== e(t) && console.error("cut：" + t + " is invalid"), h.cut = t;
            }
        },
        boundStyle: {
            default: {},
            get: function() {
                return h.boundStyle;
            },
            set: function(t) {
                "object" !== e(t) && console.error("boundStyle：" + t + " is invalid"), h.boundStyle = t;
            }
        },
        onReady: {
            default: null,
            get: function() {
                return h.ready;
            },
            set: function(t) {
                h.ready = t;
            }
        },
        onBeforeImageLoad: {
            default: null,
            get: function() {
                return h.beforeImageLoad;
            },
            set: function(t) {
                h.beforeImageLoad = t;
            }
        },
        onImageLoad: {
            default: null,
            get: function() {
                return h.imageLoad;
            },
            set: function(t) {
                h.imageLoad = t;
            }
        },
        onBeforeDraw: {
            default: null,
            get: function() {
                return h.beforeDraw;
            },
            set: function(t) {
                h.beforeDraw = t;
            }
        }
    }, f = n().windowWidth, l = "undefined" != typeof window ? window : "undefined" != typeof global ? global : "undefined" != typeof self ? self : {}, g = o(function(t, n) {
        n.isStr = function(t) {
            return "string" == typeof t;
        }, n.isNum = function(t) {
            return "number" == typeof t;
        }, n.isArr = Array.isArray, n.isUndef = function(t) {
            return void 0 === t;
        }, n.isTrue = function(t) {
            return !0 === t;
        }, n.isFalse = function(t) {
            return !1 === t;
        }, n.isFunc = function(t) {
            return "function" == typeof t;
        }, n.isObj = n.isObject = function(t) {
            return null !== t && "object" === e(t);
        };
        var o = Object.prototype.toString;
        n.isPlainObject = function(t) {
            return "[object Object]" === o.call(t);
        };
        var r = Object.prototype.hasOwnProperty;
        n.hasOwn = function(t, e) {
            return r.call(t, e);
        }, n.noop = function(t, e, n) {}, n.isValidArrayIndex = function(t) {
            var e = parseFloat(String(t));
            return e >= 0 && Math.floor(e) === e && isFinite(t);
        };
    }), p = g.isFunc, v = g.isPlainObject, x = [ "ready", "beforeImageLoad", "beforeDraw", "imageLoad" ], y = r(wx.getImageInfo), m = r(wx.canvasToTempFilePath), b = o(function(t, n) {
        !function(o) {
            var r = n, i = t && t.exports == r && t, a = "object" == e(l) && l;
            a.global !== a && a.window !== a || (o = a);
            var c = function(t) {
                this.message = t;
            };
            (c.prototype = new Error()).name = "InvalidCharacterError";
            var u = function(t) {
                throw new c(t);
            }, s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/", h = /[\t\n\f\r ]/g, d = {
                encode: function(t) {
                    t = String(t), /[^\0-\xFF]/.test(t) && u("The string to be encoded contains characters outside of the Latin1 range.");
                    for (var e, n, o, r, i = t.length % 3, a = "", c = -1, h = t.length - i; ++c < h; ) e = t.charCodeAt(c) << 16, 
                    n = t.charCodeAt(++c) << 8, o = t.charCodeAt(++c), a += s.charAt((r = e + n + o) >> 18 & 63) + s.charAt(r >> 12 & 63) + s.charAt(r >> 6 & 63) + s.charAt(63 & r);
                    return 2 == i ? (e = t.charCodeAt(c) << 8, n = t.charCodeAt(++c), a += s.charAt((r = e + n) >> 10) + s.charAt(r >> 4 & 63) + s.charAt(r << 2 & 63) + "=") : 1 == i && (r = t.charCodeAt(c), 
                    a += s.charAt(r >> 2) + s.charAt(r << 4 & 63) + "=="), a;
                },
                decode: function(t) {
                    var e = (t = String(t).replace(h, "")).length;
                    e % 4 == 0 && (e = (t = t.replace(/==?$/, "")).length), (e % 4 == 1 || /[^+a-zA-Z0-9/]/.test(t)) && u("Invalid character: the string to be decoded is not correctly encoded.");
                    for (var n, o, r = 0, i = "", a = -1; ++a < e; ) o = s.indexOf(t.charAt(a)), n = r % 4 ? 64 * n + o : o, 
                    r++ % 4 && (i += String.fromCharCode(255 & n >> (-2 * r & 6)));
                    return i;
                },
                version: "0.1.0"
            };
            if (r && !r.nodeType) if (i) i.exports = d; else for (var f in d) d.hasOwnProperty(f) && (r[f] = d[f]); else o.base64 = d;
        }(l);
    }), w = function(t, e) {
        return void 0 === t && (t = {}), void 0 === e && (e = function() {}), c(t.canvasId, t.x, t.y, t.width, t.height, "bmp", e);
    }, C = {
        touchStart: function(e) {
            var n = e.touches, o = n[0], r = n[1];
            this.src && (t(this, !0, null, null), this.__oneTouchStart(o), e.touches.length >= 2 && this.__twoTouchStart(o, r));
        },
        touchMove: function(e) {
            var n = e.touches, o = n[0], r = n[1];
            this.src && (t(this, null, !0), 1 === e.touches.length && this.__oneTouchMove(o), 
            e.touches.length >= 2 && this.__twoTouchMove(o, r));
        },
        touchEnd: function(e) {
            this.src && (t(this, !1, !1, !0), this.__xtouchEnd());
        }
    }, S = function(t) {
        var e, n, o = {};
        return e = this, n = d, Object.defineProperties(e, n), Object.keys(d).forEach(function(t) {
            o[t] = d[t].default;
        }), Object.assign(this, o, t), this.prepare(), this.attachPage(), this.createCtx(), 
        this.observer(), this.cutt(), this.methods(), this.init(), this.update(), this;
    };
    return S.prototype.init = function() {
        var e = this.src;
        return this.version = "1.3.9", "function" == typeof this.onReady && this.onReady(this.ctx, this), 
        e ? this.pushOrign(e) : this.updateCanvas(), t(this, !1, !1, !1), this.oldScale = 1, 
        this.newScale = 1, this;
    }, Object.assign(S.prototype, C), S.prototype.prepare = function() {
        var t = this;
        t.attachPage = function() {
            var e = getCurrentPages(), n = e[e.length - 1];
            Object.defineProperty(n, "wecropper", {
                get: function() {
                    return console.warn("Instance will not be automatically bound to the page after v1.4.0\n\nPlease use a custom instance name instead\n\nExample: \nthis.mycropper = new WeCropper(options)\n\n// ...\nthis.mycropper.getCropperImage()"), 
                    t;
                },
                configurable: !0
            });
        }, t.createCtx = function() {
            var e = t.id, n = t.targetId;
            e ? (t.ctx = t.ctx || wx.createCanvasContext(e), t.targetCtx = t.targetCtx || wx.createCanvasContext(n)) : console.error("constructor: create canvas context failed, 'id' must be valuable");
        }, t.deviceRadio = f / 750;
    }, S.prototype.observer = function() {
        var t = this;
        t.on = function(e, n) {
            var o;
            return x.indexOf(e) > -1 ? p(n) && ("ready" === e ? n(t) : t["on" + ((o = e).charAt(0).toUpperCase() + o.slice(1))] = n) : console.error("event: " + e + " is invalid"), 
            t;
        };
    }, S.prototype.methods = function() {
        var t = this, e = t.width, n = t.height, o = (t.bgcolor, t.id), r = t.targetId, a = t.pixelRatio, c = t.cut, u = c.x;
        void 0 === u && (u = 0);
        var s = c.y;
        void 0 === s && (s = 0);
        var h = c.width;
        void 0 === h && (h = e);
        var d = c.height;
        void 0 === d && (d = n), t.updateCanvas = function(e) {
            return t.croperTarget && (t.ctx.setFillStyle(t.bgcolor), t.ctx.fillRect(t.imgLeft, t.imgTop, t.scaleWidth, t.scaleHeight), 
            t.ctx.drawImage(t.croperTarget, t.imgLeft, t.imgTop, t.scaleWidth, t.scaleHeight)), 
            p(t.onBeforeDraw) && t.onBeforeDraw(t.ctx, t), t.setBoundStyle(t.boundStyle), t.ctx.draw(!1, e), 
            t;
        }, t.pushOrigin = t.pushOrign = function(e) {
            return t.src = e, p(t.onBeforeImageLoad) && t.onBeforeImageLoad(t.ctx, t), y({
                src: e
            }).then(function(e) {
                var n = e.width / e.height, o = h / d;
                return t.croperTarget = e.path, n < o ? (t.rectX = u, t.baseWidth = h, t.baseHeight = h / n, 
                t.rectY = s - Math.abs((d - t.baseHeight) / 2)) : (t.rectY = s, t.baseWidth = d * n, 
                t.baseHeight = d, t.rectX = u - Math.abs((h - t.baseWidth) / 2)), t.imgLeft = t.rectX, 
                t.imgTop = t.rectY, t.scaleWidth = t.baseWidth, t.scaleHeight = t.baseHeight, t.update(), 
                new Promise(function(e) {
                    t.updateCanvas(e);
                });
            }).then(function() {
                p(t.onImageLoad) && t.onImageLoad(t.ctx, t);
            });
        }, t.removeImage = function() {
            return t.src = "", t.croperTarget = "", i(t.ctx);
        }, t.getCropperBase64 = function(t) {
            void 0 === t && (t = function() {}), w({
                canvasId: o,
                x: u,
                y: s,
                width: h,
                height: d
            }, t);
        }, t.getCropperImage = function(e, n) {
            var c = e, f = {
                canvasId: o,
                x: u,
                y: s,
                width: h,
                height: d,
                destWidth: 2 * h,
                destHeight: 2 * d
            }, l = function() {
                return Promise.resolve();
            };
            return v(c) && c.original && (l = function() {
                return t.targetCtx.drawImage(t.croperTarget, t.imgLeft * a, t.imgTop * a, t.scaleWidth * a, t.scaleHeight * a), 
                f = {
                    canvasId: r,
                    x: u * a,
                    y: s * a,
                    width: h * a,
                    height: d * a
                }, i(t.targetCtx);
            }), l().then(function() {
                v(c) && (f = Object.assign({}, f, c)), p(c) && (n = c);
                var t = f.componentContext ? [ f, f.componentContext ] : [ f ];
                return m.apply(null, t);
            }).then(function(e) {
                var o = e.tempFilePath;
                return p(n) ? n.call(t, o, null) : o;
            }).catch(function(e) {
                if (!p(n)) throw e;
                n.call(t, null, e);
            });
        };
    }, S.prototype.cutt = function() {
        var t = this, e = t.width, n = t.height, o = t.cut, r = o.x;
        void 0 === r && (r = 0);
        var i = o.y;
        void 0 === i && (i = 0);
        var a = o.width;
        void 0 === a && (a = e);
        var c = o.height;
        void 0 === c && (c = n), t.outsideBound = function(e, n) {
            t.imgLeft = e >= r ? r : t.scaleWidth + e - r <= a ? r + a - t.scaleWidth : e, t.imgTop = n >= i ? i : t.scaleHeight + n - i <= c ? i + c - t.scaleHeight : n;
        }, t.setBoundStyle = function(o) {
            void 0 === o && (o = {});
            var u = o.color;
            void 0 === u && (u = "white");
            var s = o.mask;
            void 0 === s && (s = "rgba(0, 0, 0, 0.3)");
            var h = 1;
            void 0 === h && (h = 1);
            var d = [ {
                start: {
                    x: r - h,
                    y: i + 10 - h
                },
                step1: {
                    x: r - h,
                    y: i - h
                },
                step2: {
                    x: r + 10 - h,
                    y: i - h
                }
            }, {
                start: {
                    x: r - h,
                    y: i + c - 10 + h
                },
                step1: {
                    x: r - h,
                    y: i + c + h
                },
                step2: {
                    x: r + 10 - h,
                    y: i + c + h
                }
            }, {
                start: {
                    x: r + a - 10 + h,
                    y: i - h
                },
                step1: {
                    x: r + a + h,
                    y: i - h
                },
                step2: {
                    x: r + a + h,
                    y: i + 10 - h
                }
            }, {
                start: {
                    x: r + a + h,
                    y: i + c - 10 + h
                },
                step1: {
                    x: r + a + h,
                    y: i + c + h
                },
                step2: {
                    x: r + a - 10 + h,
                    y: i + c + h
                }
            } ];
            t.ctx.beginPath(), t.ctx.setFillStyle(s), t.ctx.fillRect(0, 0, r, n), t.ctx.fillRect(r, 0, a, i), 
            t.ctx.fillRect(r, i + c, a, n - i - c), t.ctx.fillRect(r + a, 0, e - r - a, n), 
            t.ctx.fill(), d.forEach(function(e) {
                t.ctx.beginPath(), t.ctx.setStrokeStyle(u), t.ctx.setLineWidth(h), t.ctx.moveTo(e.start.x, e.start.y), 
                t.ctx.lineTo(e.step1.x, e.step1.y), t.ctx.lineTo(e.step2.x, e.step2.y), t.ctx.stroke();
            });
        };
    }, S.prototype.update = function() {
        var t = this;
        t.src && (t.__oneTouchStart = function(e) {
            t.touchX0 = Math.round(e.x), t.touchY0 = Math.round(e.y);
        }, t.__oneTouchMove = function(e) {
            var n, o;
            if (t.touchended) return t.updateCanvas();
            n = Math.round(e.x - t.touchX0), o = Math.round(e.y - t.touchY0);
            var r = Math.round(t.rectX + n), i = Math.round(t.rectY + o);
            t.outsideBound(r, i), t.updateCanvas();
        }, t.__twoTouchStart = function(e, n) {
            var o, r, i;
            t.touchX1 = Math.round(t.rectX + t.scaleWidth / 2), t.touchY1 = Math.round(t.rectY + t.scaleHeight / 2), 
            o = Math.round(n.x - e.x), r = Math.round(n.y - e.y), i = Math.round(Math.sqrt(o * o + r * r)), 
            t.oldDistance = i;
        }, t.__twoTouchMove = function(e, n) {
            var o = t.oldScale, r = t.oldDistance, i = t.scale, a = t.zoom;
            t.newScale = function(t, e, n, o, r) {
                var i, a;
                return i = Math.round(r.x - o.x), a = Math.round(r.y - o.y), t + .001 * n * (Math.round(Math.sqrt(i * i + a * a)) - e);
            }(o, r, a, e, n), t.newScale <= 1 && (t.newScale = 1), t.newScale >= i && (t.newScale = i), 
            t.scaleWidth = Math.round(t.newScale * t.baseWidth), t.scaleHeight = Math.round(t.newScale * t.baseHeight);
            var c = Math.round(t.touchX1 - t.scaleWidth / 2), u = Math.round(t.touchY1 - t.scaleHeight / 2);
            t.outsideBound(c, u), t.updateCanvas();
        }, t.__xtouchEnd = function() {
            t.oldScale = t.newScale, t.rectX = t.imgLeft, t.rectY = t.imgTop;
        });
    }, S;
}, "object" === ("undefined" == typeof exports ? "undefined" : e(exports)) && "undefined" != typeof module ? module.exports = t() : "function" == typeof define && define.amd ? define(t) : (void 0).WeCropper = t();